//
//  ZFSignAlertView.swift
//  map
//
//  Created by fyzq on 2024/2/1.
//

import UIKit
import RxSwift
import Turf
import BetterSegmentedControl
class ZFSignAlertView: ZFBaseView {

    private var locationLabel:UILabel!
    private var distanceLabel:UILabel!
    private var wgsLabel:UILabel!
    var signBottomView:ZFSignBottomView!
    
    var elevation:Double?
    var location:CLLocationCoordinate2D!{
        didSet{
            ZFAMapManager.shared.onReGeocodeSearch(location: location).subscribe(onNext:{[weak self]modal in
                guard let self = self else {return}
                self.locationLabel.text = modal.formattedAddress;
            }).disposed(by: self.dis)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFSignAlertView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFSignAlertView(){
        backgroundColor = R.color.bg()
        
        locationLabel = UILabel()
        locationLabel.lineBreakMode = .byWordWrapping
        locationLabel.numberOfLines = 0
        locationLabel.textColor = R.color.color_21()
        locationLabel.font = UIFont.boldSystemFont(ofSize: 15)
        addSubview(locationLabel)
        locationLabel.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            
        }
        
        distanceLabel  = UILabel()
        distanceLabel.textColor = R.color.color_3c()
        distanceLabel.font = UIFont.iconfont(size: 14)
        addSubview(distanceLabel)
        distanceLabel.snp.makeConstraints { make in
            make.top.equalTo(locationLabel.snp.bottom).offset(10)
            make.left.right.equalTo(locationLabel)
        }
        
        wgsLabel  = UILabel()
        wgsLabel.textColor = R.color.color_3c()
        wgsLabel.font = UIFont.iconfont(size: 14)
        addSubview(wgsLabel)
        wgsLabel.snp.makeConstraints { make in
            make.top.equalTo(distanceLabel.snp.bottom).offset(15)
            make.left.right.equalTo(locationLabel)
        }
        
        let segments = LabelSegment.segments(withTitles: ["WGS84", "GCJ-02","BD-09"],normalFont: UIFont.systemFont(ofSize: 11),normalTextColor:R.color.color_66(), selectedFont: UIFont.systemFont(ofSize: 11),selectedTextColor: .white)
        
        let segmentedControl =  BetterSegmentedControl(frame: CGRect.zero, segments:segments,options:
                                                        [.indicatorViewBackgroundColor(R.color.accentColor()!),
                                                         .cornerRadius(15.0),])
        addSubview(segmentedControl)
        segmentedControl.snp.makeConstraints { make in
            make.top.equalTo(wgsLabel.snp.bottom).offset(15)
            make.left.right.equalTo(locationLabel)
        }
        
        
        
        signBottomView = ZFSignBottomView()
        addSubview(signBottomView)
        signBottomView.snp.makeConstraints { make in
            make.top.equalTo(segmentedControl.snp.bottom).offset(20)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-UIDevice.safeAreaInsets.bottom - 10)
        }
        segmentedControl.rx.controlEvent(.valueChanged).subscribe(on: MainScheduler.instance).subscribe(onNext:{[weak segmentedControl]_ in
            guard let control = segmentedControl else {
                return;
            }
            UIView.feedback()
            if control.index == 0 {
                self.wgsLabel.text = "WGS84 :\(ZFUtils.convertToDegreesMinutesSeconds(from: self.location.longitude)) , \(ZFUtils.convertToDegreesMinutesSeconds(from: self.location.latitude)) | 复制"
            }else if control.index == 1 {
                let location = self.location.wgs84togcj02()
                self.wgsLabel.text = "GCJ-02 :\(ZFUtils.convertToDegreesMinutesSeconds(from: location.longitude)) , \(ZFUtils.convertToDegreesMinutesSeconds(from: location.latitude)) | 复制"
            }else {
                let location = self.location.wgs84ToBd09()
                self.wgsLabel.text = "BD-09 :\(ZFUtils.convertToDegreesMinutesSeconds(from: location.longitude)) , \(ZFUtils.convertToDegreesMinutesSeconds(from: location.latitude)) | 复制"
            }

        }).disposed(by: dis);
        
        ZFAMapManager.requestLocation().subscribe(onNext: {[weak self]location in
            guard let self = self else {return}
            let distance = location.coordinate.distance(to: self.location)
            guard let elevation = self.elevation else {
                return
            }
            self.distanceLabel.text = distance > 10000 ? String(format: "距您%.2f km ，海拔：%.2f m", distance / 1000,elevation) : String(format: "距您%.2f m，海拔：%.2f m", distance,elevation)
            self.wgsLabel.text = "WGS84 :\(ZFUtils.convertToDegreesMinutesSeconds(from: self.location.longitude)) , \(ZFUtils.convertToDegreesMinutesSeconds(from: self.location.latitude)) | 复制"
            
        }).disposed(by: self.dis)
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        jk.addCorner(conrners: [UIRectCorner.topLeft,UIRectCorner.topRight], radius: 10)
    }
}



class ZFSignBottomView : ZFBaseView {
    var subject = PublishSubject<Int>()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFSignBottomView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFSignBottomView(){
        
        let pathBtn = ZFIconBtn()
        pathBtn.iconLabel.text = .icon(icon: .ic_navigation)
        pathBtn.textLabel.text = "导航"
    
        let streetBtn = ZFIconBtn()
        streetBtn.iconLabel.text = .icon(icon: .ic_street)
        streetBtn.textLabel.text = "街景"
        
        let sharetBtn = ZFIconBtn()
        sharetBtn.iconLabel.text = .icon(icon: .ic_share)
        sharetBtn.textLabel.text = "分享"
        
        
        let addBtn = UIButton(type: UIButton.ButtonType.custom)
        addBtn.setTitle("+添加兴趣点", for: UIControl.State.normal)
        addBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        addBtn.backgroundColor = R.color.accentColor()
        addBtn.setTitleColor(.white, for: UIControl.State.normal)
        addBtn.snp.makeConstraints { make in
            make.width.equalTo(200)
            make.height.equalTo(40)
        }
        addBtn.addRadius(radius: 20)
        
        
        let stackView =  UIStackView(arrangedSubviews: [pathBtn,streetBtn,sharetBtn,addBtn])
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .equalSpacing
        stackView.spacing = 20
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20))
        }
        
        pathBtn.rx.tapGesture().skip(1).subscribe(onNext: {[weak self]_ in
            guard let self = self else {return}
            UIView.feedback()
            self.subject.onNext(0)
        }).disposed(by: dis)
        
        streetBtn.rx.tapGesture().skip(1).subscribe(onNext: {[weak self]_ in
            guard let self = self else {return}
            UIView.feedback()
            self.subject.onNext(1)
        }).disposed(by: dis)
        
        sharetBtn.rx.tapGesture().skip(1).subscribe(onNext: {[weak self]_ in
            guard let self = self else {return}
            UIView.feedback()
            self.subject.onNext(2)
        }).disposed(by: dis)
        
        addBtn.rx.tap.subscribe(onNext: {[weak self]_ in
            guard let self = self else {return}
            UIView.feedback()
            self.subject.onNext(3)
        }).disposed(by: dis)
        

        
    }
    
}

class ZFIconBtn :ZFBaseView {
    
    var iconLabel : UILabel!
    var textLabel : UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFTopBtn()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFTopBtn(){
        
        iconLabel =  UILabel()
        iconLabel.textColor = R.color.color_33()
        iconLabel.font = UIFont.iconfont(size: 22)
        
        textLabel =  UILabel()
        textLabel.textColor = R.color.color_66()
        textLabel.font = UIFont.systemFont(ofSize: 14)
        
        let stackView =  UIStackView(arrangedSubviews: [iconLabel,textLabel])
        stackView.axis = .vertical
        stackView.alignment = .center
        stackView.spacing = 5
        stackView.distribution = .equalCentering
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

